﻿Public Class 欠账用户查询

    Private database As DataAccess
    Private data_Out As DataTable

    Private Sub 欠账用户查询_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        database = New DataAccess
        database.OpenConnection()

        Dim sqlString As String
        sqlString = "select * from tf_out_goods"
        data_Out = database.QueryDatabase(sqlString)

        query_owing_record()

    End Sub

    Private Sub query_owing_record()
        '生成数据过滤条件
        Dim filter As String = "owing_tag=1"

        '在dataview中显示过滤数据
        Dim dv As DataView
        dv = data_Out.DefaultView
        DataGridView_Query.DataSource = dv
        dv.RowFilter = filter

        '修改列标题
        DataGridView_Query.Columns(0).HeaderText = "出库批次"
        DataGridView_Query.Columns(1).HeaderText = "商品编号"
        DataGridView_Query.Columns(2).HeaderText = "商品种类"
        DataGridView_Query.Columns(3).HeaderText = "商品名称"
        DataGridView_Query.Columns(4).HeaderText = "出货价"
        DataGridView_Query.Columns(5).HeaderText = "出库数量"
        DataGridView_Query.Columns(6).HeaderText = "计量单位"
        DataGridView_Query.Columns(7).HeaderText = "是否欠账"
        DataGridView_Query.Columns(8).HeaderText = "是否退货"
        DataGridView_Query.Columns(9).HeaderText = "出货时间"
        DataGridView_Query.Columns(10).HeaderText = "修改时间"
        DataGridView_Query.Columns(11).HeaderText = "备注"

        '隐藏部分列
        DataGridView_Query.Columns(0).Visible = False
        DataGridView_Query.Columns(1).Visible = False
        DataGridView_Query.Columns(2).Visible = True
        DataGridView_Query.Columns(3).Visible = True
        DataGridView_Query.Columns(4).Visible = True
        DataGridView_Query.Columns(5).Visible = True
        DataGridView_Query.Columns(6).Visible = False
        DataGridView_Query.Columns(7).Visible = False
        DataGridView_Query.Columns(8).Visible = False
        DataGridView_Query.Columns(9).Visible = False
        DataGridView_Query.Columns(10).Visible = False
        DataGridView_Query.Columns(11).Visible = True

    End Sub

    Private Sub Button_Owing_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Owing_Cancel.Click

        Dim good_name As String = DataGridView_Query.CurrentRow.Cells(3).Value
        Dim good_price As Decimal = DataGridView_Query.CurrentRow.Cells(4).Value
        Dim good_number As Decimal = DataGridView_Query.CurrentRow.Cells(5).Value
        Dim total_price As Decimal = good_price * good_number
        Dim remark As String = DataGridView_Query.CurrentRow.Cells(11).Value

        Dim message As String
        message = "是否确定销账: " & good_name & vbCrLf & "总价格: " & total_price & vbCrLf & remark

        If MsgBox(message, MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
            Return
        End If

        '更新商品出库表
        Dim good_id As Integer
        good_id = DataGridView_Query.CurrentRow.Cells(1).Value

        Dim row As DataRow
        For Each row In data_Out.Rows
            If good_id = row.Item("goods_id") Then
                row("owing_tag") = 0
            End If
        Next row

        If database.UpdateDatabase(data_Out) = False Then
            MessageBox.Show("更新商品数据库时失败")
        End If

    End Sub
End Class